package com.ms.content.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.ms.common.utils.R;
import com.ms.content.entity.BargainingItemEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ms.common.utils.PageUtils;
import com.ms.common.utils.Query;

import com.ms.content.dao.GoodsDao;
import com.ms.content.entity.GoodsEntity;
import com.ms.content.service.GoodsService;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(
                new Query<GoodsEntity>().getPage(params),
                new QueryWrapper<GoodsEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R seleById(Integer id) {
        QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<GoodsEntity>().eq("id", id);
        GoodsEntity goodsEntity = baseMapper.selectOne(wrapper);
        if (goodsEntity==null){
            return  R.error("后台商品发生错误");
        }
        return R.ok().put("entity",goodsEntity);
    }

    @Override
    public void deleteById(Integer id) {
        QueryWrapper<GoodsEntity> wrapper = new QueryWrapper<GoodsEntity>().eq("id", id);
        GoodsEntity goodsEntity = baseMapper.selectOne(wrapper);
        baseMapper.deleteById(goodsEntity);
    }

    @Override
    public GoodsEntity seleByIdsj(Integer id) {
        System.out.println(id);
        QueryWrapper<GoodsEntity> id1 = new QueryWrapper<GoodsEntity>().eq("id", id);
        GoodsEntity goodsEntity = baseMapper.selectOne(id1);
        return goodsEntity;
    }

//    @Override
//    public Integer selectCount(Integer sid) {
//        return baseMapper.selectount(sid);
//    }

    @Override
    public List<GoodsEntity> seleList() {
        List<GoodsEntity> goodsEntities = baseMapper.selectList(null);
        return goodsEntities;
    }

    @Override
    public R showGoods(String uuid) {
        String s = stringRedisTemplate.opsForValue().get(uuid);
        BargainingItemEntity bargainingItemEntity = JSONObject.parseObject(s, new TypeReference<BargainingItemEntity>() {
        });
        String s1 = stringRedisTemplate.opsForValue().get(bargainingItemEntity.getGoodsId() + "," + bargainingItemEntity.getUid());
        List<GoodsEntity> goodsEntities = JSONObject.parseObject(s1, new TypeReference<List<GoodsEntity>>() {
        });
        //获取最近一次更新的数据
        GoodsEntity goodsEntity = goodsEntities.get(goodsEntities.size() - 1);
        return R.ok().put("goodsEntity",goodsEntity);
    }

}